package com.example.baseadapter;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnItemClickListener {

	/*
	 * Create custom adapter We are taking data source from a string array in
	 * strings.xml class MyCustomAdapter extends BaseAdapter { public int
	 * getCount() { //return number of elements inside the array } public Object
	 * getItem(int arg0) { //return the object at position at arg0 } public long
	 * getItemId(int position) { return the id of the row which is our array
	 * index itself in this case } public View getView(int position,View
	 * convertView,ViewGroup parent) { this is the view for the single list item
	 * which is inside the xml file. } }
	 */

	private ListView list;
	private ActionBar actionBar;
	private DrawerLayout drawerLayout;
	private ActionBarDrawerToggle drawerListener;
	private ListView listView;
	private String[] navDrawerListViewItem;
	private RelativeLayout leftRL;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color
				.rgb(10,132,132)));
		setContentView(R.layout.activity_main);

		list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(new Adapter(this));

		leftRL = (RelativeLayout) findViewById(R.id.whatYouWantInLeftDrawer);
		drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
		// string array is defined on the strings.xml
		navDrawerListViewItem = getResources().getStringArray(
				R.array.navDrawerListViewItem);
		listView = (ListView) findViewById(R.id.drawerList);
		// adapter will load data from string array to thr list view.
		listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, navDrawerListViewItem));
		listView.setOnItemClickListener(this);
		// this listener will set three bar image on the action
		// bar.ActionBarToggle class use this listener.
		drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			@Override
			public void onDrawerClosed(View drawerView) {

			}

			@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
			@Override
			public void onDrawerOpened(View drawerView) {
				drawerLayout.openDrawer(leftRL);
			}
		};
		drawerLayout.setDrawerListener(drawerListener);
		// this method will set the action bar app icon clickable.
		getSupportActionBar().setHomeButtonEnabled(true);
		// this method will set back button on app icon for the home screen.
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long l) {
				Case dataModel = (Case) adapterView.getItemAtPosition(position);
				Intent i = new Intent(MainActivity.this, CaseDetail.class);
				int id = dataModel.caseId;
				i.putExtra("id", id);
				startActivity(i);
			}

		});
	}

	// this method will set the three bar icon in the action bar.
	@Override
	public void onPostCreate(Bundle savedInstance) {
		super.onPostCreate(savedInstance);
		drawerListener.syncState();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.action_bar_icons, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * On selecting action bar icons
	 * */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// nav drawerToggle on app icon click
		if (drawerListener.onOptionsItemSelected(item)) {
			return true;
		}

		// Take appropriate action for each action item click
		switch (item.getItemId()) {
		case R.id.action_addNew:
			// adding new icon in the action bar
			addNewIconAction();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

	private void addNewIconAction() {
		Intent i = new Intent(MainActivity.this, AddCase.class);
		startActivity(i);

	}

	@Override
	public void onBackPressed() {
		exit();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		int itemPosition = position;

		// ListView Clicked item value
		String itemValue = (String) listView.getItemAtPosition(position);

		// Show Alert
		Toast.makeText(getApplicationContext(),
				"Position :" + itemPosition + "  ListItem : " + itemValue,
				Toast.LENGTH_LONG).show();

		switch (itemPosition) {
		case 0:
			Intent i1 = new Intent(this, AddCase.class);
			startActivity(i1);
			break;
		case 1:
			Intent i8 = new Intent(this, AddCase.class);
			startActivity(i8);
			break;
		case 2:
			Intent i3 = new Intent(this, MainActivity.class);
			startActivity(i3);
			break;
		case 3:
			Intent i4 = new Intent(this, AddCase.class);
			startActivity(i4);
			break;
		case 4:
			Intent i5 = new Intent(this, AddCase.class);
			startActivity(i5);
			break;
		case 5:
			Intent i6 = new Intent(this, About.class);
			startActivity(i6);
			break;
		case 6:
			exit();
			break;
		}

	}

	public void exit() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				MainActivity.this);

		// set title
		alertDialogBuilder.setTitle("Do you want to exit ??");

		// set dialog message
		alertDialogBuilder
				.setMessage("Click yes to exit!")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// if this button is clicked, close
								// current activity
								MainActivity.this.finish();
								
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
						drawerLayout.closeDrawers();
					}
				});

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();
	}

}
